package com.sumainfor.pc.market.controller.rentadviser;

import com.sumainfor.common.entity.ParentDeptVo;
import com.sumainfor.common.entity.SysUserVo;
import com.sumainfor.common.util.ContextUtils;
import com.sumainfor.common.util.FieldCheckUtil;
import com.sumainfor.common.util.JsonResult;
import com.sumainfor.common.utlis.PageUtils;
import com.sumainfor.common.utlis.Pager;
import com.sumainfor.pc.market.dao.HousingMapper;
import com.sumainfor.pc.market.service.RdHousingService;
import com.sumainfor.pc.market.service.RdTenantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Date 2018年11月7日
 * @Describe 市场部房源配置查询
 */
@RestController
@RequestMapping("/rentadviser/RdHousingController")
public class RdHousingController implements Serializable {
    @Autowired
    private RdHousingService rdHousingService;
    @Autowired
    private RdTenantService rdTenantService;

    @Autowired
    private HousingMapper housingMapper;
    /**
     *分页查询房源池的所有数据
     * @param
     * @return
     */
    @PostMapping("/getCommunityList")
    public JsonResult getRepairList(@RequestBody Map<String,Object>params, Pager pager){
        //获取当前登录人的服务中心编号
        SysUserVo userVo = ContextUtils.getSysUser();
        ParentDeptVo vo = null;
        for (int i = 0; i < userVo.getLength(); i++) {
            ParentDeptVo vo1 = userVo.getParentDeptVo();
            if (vo1.getType().equals(3)) {
                vo = vo1;
                break;
            }
        }
        if (vo!=null){
            params.put("area_id",vo.getDeptId());//当前登录人的服务中心编号
            System.out.println("area_id:"+vo.getDeptId());
        }
        JsonResult result=new JsonResult();
        pager.setPageno(Integer.parseInt(params.get("pageno").toString()));
        pager.setPagesize(Integer.parseInt(params.get("pagesize").toString()));
        pager.setPagerNecessary(params, pager);
        PageUtils pageUtils = new PageUtils();
        String a=params.get("inIdList").toString();
        String b=params.get("towardList").toString();
        String c=params.get("roomsListh").toString();
        String d=params.get("roomsListz").toString();
        String e=params.get("stateList").toString();

        //服务中心经理
        //根据房源状态筛选房源
        if (a.equals("")) {//介绍标签
            params.put("inIdList", null);
        }else{
            params.put("inIdList",  a.split(","));
        }
        if (b.equals("")) {//房源朝向
            params.put("towardList", null);
        }else{
            params.put("towardList", b.split(","));
        }
        if (c.equals("")) {//唐巢合租
            params.put("roomsListh", null);
        }else{
            params.put("roomsListh", c.split(","));
        }
        if (d.equals("")) {//唐巢整租
            params.put("roomsListz", null);
        }else{
            params.put("roomsListz", d.split(","));
        }
        if (e.equals("")) {//房源状态
            params.put("stateList", null);
        }else{
            params.put("stateList", e.split(","));
        }
        Integer cout=rdHousingService.getCommunityListCount(params);
        if(cout==0){
            return pageUtils.getJsonResult(new ArrayList<Map<String,Object>>(), params,cout);
        }
        List<Map<String,Object>>getCommunityList=rdHousingService.getCommunityList(params);
        result=pageUtils.getJsonResult(getCommunityList, params, cout);
        return result;
    }
    /**
     * 根据房源编号查询房源信息
     */
    @GetMapping("/getHousingByHouId")
    public JsonResult getHousingByHouId(@RequestParam Map<String,Object> params){
        JsonResult result=new JsonResult();
        Map<String,Object> map=rdHousingService.getHousingByHouId(params);
        if (map!=null){
            result.put(map);
        }else{
            result.putFailed("服务器繁忙");
        }
        return  result;
    }

    /**
     * 运营部租后主管维修跟踪查询维修工单的房源信息
     * @param params
     * @return
     */
    @PostMapping("/key/getHouInfoListForRepair")
    public JsonResult getHouInfoListForRepair(@RequestBody Map<String, Object> params) {

        List<Map<String,Object>> tList=new ArrayList<Map<String,Object>>();
        if(params.get("RepType").toString().equals("1")){//1.普通用户，2.管家
            tList= rdTenantService.getTenUserList(params);
        }
        JsonResult result=new JsonResult();
        List<Map<String,Object>>list=rdHousingService.getHouInfoListForRepair(params);
        if (list!=null){
            result.put(list);
        }else{
            result.putFailed("服务器繁忙");
        }
        result.put("tList",tList);
        return result;
    }

    /**
     * 运营部租后主管维修跟踪查询维修工单的房源信息
     * @param params
     * @return
     */
    @GetMapping("/getHouInfoListForButler")
    public JsonResult getHouInfoListForButler(@RequestParam Map<String, Object> params,
                                              Pager pager) {
        System.out.println("接收到的参数:"+params);
        //如果模糊搜索的字段有楼号, 用项目id和楼号室号去查询所有的房源id
        if (FieldCheckUtil.isMapFieldNotNull(params, new String[]{"BuilDing"})) {
            List<String> houseIdList = housingMapper.getHouseIdListByBuildAndRoom(params);
            if (houseIdList != null && houseIdList.size() > 0) {
                params.put("houIdList", houseIdList);
            }
        }

        String a=params.get("ifOperaType").toString();
        String b=params.get("state").toString();
        if (a.equals("")) {//房源租赁类型
            params.put("pTypeList", null);
        }else{
            params.put("pTypeList",  a.split(","));
        }
        if (b.equals("")) {//房源状态
            params.put("stateList", null);
        }else{
            params.put("stateList", b.split(","));
        }
        JsonResult result=new JsonResult();
        pager.setPagerNecessary(params, pager);
        PageUtils pageUtils = new PageUtils();
        Integer cout=rdHousingService.getHouInfoListCountForButler(params);
        if(cout==0){
            return pageUtils.getJsonResult(new ArrayList<Map<String,Object>>(), params,cout);
        }
        List<Map<String,Object>>getCommunityList=rdHousingService.getHouInfoListForButler(params);
        result=pageUtils.getJsonResult(getCommunityList, params, cout);
        return result;
    }
    /**
     * 查询整套房源的图片
     */
    @GetMapping("/getHouImage")
    public JsonResult getHouImage(@RequestParam Map<String,Object> params){
        JsonResult result=new JsonResult();
        Map<String,Object> map=rdHousingService.getHouImage(params);
        if (map!=null){
            result.put(map);
        }else{
            result.putFailed("服务器繁忙");
        }
        return  result;
    }
    /**
     * 租赁顾问修改房源主表图片
     */
    @PostMapping("/updateHousingImg")
    public JsonResult updateHousingImg(@RequestBody Map<String,Object> params){
        JsonResult result=new JsonResult();

        if (params.get("HouseImg")==null || params.get("ShowImg")==null){
            return result.putFailed("参数不正确");
        }
        String HouseImg=params.get("HouseImg").toString();
        String ShowImg=params.get("ShowImg").toString();
        if (HouseImg.equals("")){
            params.put("HouseImg",null);
        }
        if (ShowImg.equals("")){
            params.put("ShowImg",null);
        }
        return  rdHousingService.updateHousingImg(params);
    }
    /*运营部分配服务管家*/
    @PostMapping("/updateHousingSysUserId")
    public JsonResult updateHousingSysUserId(@RequestBody Map<String, Object> params) {
        return rdHousingService.updateHousingSysUserId(params);
    }
    /**
     * 运营部服务管家录入水电燃表数查询房源的信息
     */
    @PostMapping("/key/getHoList")
    public JsonResult getHoList(@RequestBody Map<String,Object>params){
        JsonResult result=new JsonResult();
        Pager pager=new Pager();
        pager.setPageno(Integer.parseInt(params.get("pageno").toString()));
        pager.setPagesize(Integer.parseInt(params.get("pagesize").toString()));
        pager.setPagerNecessary(params, pager);
        PageUtils pageUtils = new PageUtils();
        Integer cout=rdHousingService.getHoListCount(params);
        if(cout==0){
            return pageUtils.getJsonResult(new ArrayList<Map<String,Object>>(), params,cout);
        }
        List<Map<String,Object>>getCommunityList=rdHousingService.getHoList(params);
        result=pageUtils.getJsonResult(getCommunityList, params, cout);
        return result;
    }
    /**
     * 服务管家验房时上传图片
     */
    @PostMapping("/updateHoHousingImg")
    public JsonResult updateHoHousingImg(@RequestBody Map<String,Object> params){
        JsonResult result=new JsonResult();

        if (params.get("PlansId")==null){
            return result.putFailed("参数不正确");
        }
        return  rdHousingService.updateHoHousingImg(params);
    }
    /**
     * 服务管家抄表的时候查询房源居住中的租客编号
     */
    @PostMapping("/getTenantListByTenId")
    public JsonResult getTenantListByTenId(@RequestBody Map<String,Object> params){
        return rdHousingService.getTenantListByTenId(params);
    }
    /**
     * 租后主管查询员工的信息列表（服务管家和服务秘书）
     */
    @GetMapping("/key/getRdStaffList")
    public JsonResult getRdStaffList(@RequestParam Map<String,Object> params){
        return rdHousingService.getRdStaffList(params);
    }
    /**
     * 租后主管查询员工的信息列表（服务管家和服务秘书）
     */
    @PostMapping("/key/getRdAlterList")
    public JsonResult getRdAlterList(@RequestBody Map<String,Object> params){
        return rdHousingService.getRdAlterList(params);
    }

}
